package net.googlecode.harchenko.task4;

import java.util.StringTokenizer;
import java.util.Calendar;
import java.text.SimpleDateFormat;


/**
 * Created by IntelliJ IDEA.
 * User: undertext
 * Date: 03.04.11
 * Time: 13:33
 * To change this template use File | Settings | File Templates.
 */

/**
 * Class for task4. Realize methods of transformation of string.
 *
 * @author Kharchenko Yaroslav
 */
public class StringProcessor {

    /**
     * Constant string, that is operand in methods of class.
     */
    public static final String STRING =
            "hello       WORLD,   I'm  cooL  \t \n ProGrAMMisT.";

    private final static Integer CHANGE_DATE = -20;


    /**
     * Reformat string to string with normal register.
     * @param str String to be modified.
     */
    private void normalizeRegister(StringBuffer str) {

        str.setCharAt(0,
                Character.toUpperCase(str.charAt(0)));

        for (int i = 1; i < str.length(); i++) {
            str.setCharAt(i,
            Character.toLowerCase(str.charAt(i)));
        }

    }

      /**
     * Normalize string. Sentence begins with capital letter,
     * normal register of symbols, remove superfluous blanks,
     * signs of tabulation and transfers into a new line.
     *
     * @return Normalizing string.
     */

    public String normalizeString() {

        final StringTokenizer strtok = new StringTokenizer(STRING, " \t\n\r");

        final StringBuffer outputString = new StringBuffer(STRING.length());
        while (strtok.hasMoreTokens()) {
            if (outputString.length() != 0) {
                outputString.append(" ");
            }
            outputString.append(strtok.nextToken());
            normalizeRegister(outputString);

        }
        return outputString.toString();


    }

    /**
     * Each first letter in a word makes in the top register.
     *
     * @return Modified String.
     */
    public String upperCaseWords() {


        final StringTokenizer strtok = new StringTokenizer(STRING, " \t\n\r.,:", true);

        final StringBuffer outputString = new StringBuffer(STRING.length());
        while (strtok.hasMoreTokens()) {
            String current = strtok.nextToken();

            StringBuffer str = new StringBuffer(current);
            str.setCharAt(0, Character.toUpperCase(str.charAt(0)));

            outputString.append(str);
        }

        return outputString.toString();


    }

    /**
     * Reverse letters in each word of sentence.
     *
     * @return Modified String.
     */
    public String reverseWords() {

        final StringTokenizer strtok = new StringTokenizer(STRING, " \t\n\r.,:", true);

        final StringBuffer outputString = new StringBuffer(STRING.length());
        while (strtok.hasMoreTokens()) {
            outputString.append(new StringBuffer(strtok.nextToken()).reverse());
        }

        return outputString.toString();

    }

    /**
     * Replace all letters 'A' or 'a' in the text with  '!'.
     *
     * @return Modified String.
     */
    public String replaceA() {
        return STRING.replaceAll("A|a", "!");
    }

    /**
     * In the end of the text, after a point, add date
     * (20 days earlier, than current date) in a format "dd-MM-yyyy hh:mm".
     *
     * @return Modified String.
     */
    public String addDate() {
        final SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm");
        final Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, CHANGE_DATE);
        return STRING + sdf.format(cal.getTime());
    }


}
